package source.view;

import source.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class StructureOverview extends JPanel{
        

        /********* DATA MEMBERS **********/
        private PlayerStatus player;

        //This is the number of different Structure Attributes
        private final int numStructuresAttributes = 8;

	private Controller controller;

	private Base[] playersStructures;

	class ourTable extends JTable{
		public ourTable(Object[][] data, String[] columnNames){
			super(data, columnNames);
			super.addKeyListener(new tableKeys());
		}
	}

	private ourTable table;

	class tableKeys implements KeyListener{
		public void keyPressed(KeyEvent e){
			switch(e.getKeyCode()){
				case KeyEvent.VK_B:
					Base[] groupbases = new Base[playersStructures.length];
					int groupbaseindex = 0;
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i)){
							groupbases[groupbaseindex] = playersStructures[i];
							groupbaseindex++;
						}
					}

					if(groupbaseindex > 0){
						Base[] groupbases2 = new Base[groupbaseindex];
						
						System.arraycopy(groupbases, 0, groupbases2, 0, groupbaseindex);
						BaseGroup b = new BaseGroup(groupbases2, player);
						updateOverview();
					}
					break;
				case KeyEvent.VK_C:
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i))
							playersStructures[i].beginBuilding(UnitType.COLONIST);
					}
					updateOverview();
					break;
				case KeyEvent.VK_E:
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i))
							playersStructures[i].beginBuilding(UnitType.EXPLORER);
					}
					updateOverview();
					break;
				case KeyEvent.VK_M:
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i))
							playersStructures[i].beginBuilding(UnitType.MELEE);
					}
					updateOverview();
					break;
				case KeyEvent.VK_R:
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i))
							playersStructures[i].beginBuilding(UnitType.RANGED);
					}
					updateOverview();
					break;
				case KeyEvent.VK_D:
					for(int i = 0; i < playersStructures.length; i++){
						if(table.isRowSelected(i))
							if(playersStructures[i].getStructureType() == StructureType.BASEGROUP)
								playersStructures[i].destroy();
					}
					updateOverview();
					break;
				default:
					controller.keyPressed(e);
					break;
			}
		}

		public void keyReleased(KeyEvent e){
		}

		public void keyTyped(KeyEvent e){
		}

	}

	class uneditableCellsModel extends AbstractTableModel{
		private String[] columnNames;
		private Object[][] rowData;

		public uneditableCellsModel(String[] columnsNames, Object[][] data){
			this.columnNames = columnsNames;
			this.rowData = data;
		}

		public boolean isCellEditable(int row, int col){
			return false;
		}

		public String getColumnName(int col){
			return columnNames[col].toString();
		}

		public int getRowCount(){
			return rowData.length;
		}

		public int getColumnCount(){
			return columnNames.length;
		}

		public Object getValueAt(int row, int col){
			return rowData[row][col];
		}

		public void setValueAt(Object value, int row, int col){
			rowData[row][col] = value;
			fireTableCellUpdated(row, col);
		}
	}

	
	

        /********** CONSTRUCTORS **********/
        public StructureOverview(){
                player = null;

        }

        public StructureOverview(PlayerStatus player){
                this.player = player;
        }
        
	public StructureOverview(Controller controller){
		this.controller = controller;
	}

        /********** PUBLIC FUNCTIONS ***********/
	public void setCurrentPlayer(PlayerStatus player){
		this.player = player;
	}

        public void updateOverview(){
                //Clears the current overview
                removeAll();
                
                if(player == null)
                        return;

		setLayout(new GridLayout(1, 1));

                playersStructures = player.getBasesArray();

		String[] columnNames = {"Type", "CurHP/MaxHP", "View Range", "Off. Damage", "Def. Damage", "X,Y", "Prod Rate", "Job"};

		final Object[][] data = new Object[playersStructures.length][numStructuresAttributes];

		for(int i = 0; i < playersStructures.length; i++){
			Base base = playersStructures[i];
	
			if(base.getStructureType() == StructureType.BASE)
				data[i][0] = "Base";
			else
				data[i][0] = "Base Group";
	
			data[i][1] = base.getCurrentHP() + "/" + base.getMaxHP();
	
			data[i][2] = base.getViewDistance() + "";
	
			data[i][3] = base.getAttackPower() + "";

			data[i][4] = base.getDefensivePower() + "";

			if(base.getStructureType() == StructureType.BASE)
				data[i][5] = base.getPosition().getX() + "," + base.getPosition().getY();
			else
				data[i][5] = "-,-";

			data[i][6] = base.getProductionRate() + "";

			String unitName = "";
			UnitType uType = base.beingBuilt();
			if(uType == null){
				unitName = "None";
			}else{
				switch(uType){
					case COLONIST:
						unitName = "Colonist";
						break;
					case EXPLORER:
						unitName = "Explorer";
						break;
					case MELEE:
						unitName = "Melee";
						break;
					case RANGED:
						unitName = "Ranged";
						break;
				}
			}
			data[i][7] = unitName;
		}
	
		table = new ourTable(data, columnNames);
		uneditableCellsModel ucm = new uneditableCellsModel(columnNames, data);
		table.setModel(ucm);
		table.removeEditor();

		JScrollPane scrollpane = new JScrollPane(table);
		add(scrollpane);
		validate();
		repaint();
	}
                
}

